//
//  MyCell.m
//  MyGestureCell
//
//  Created by 黄世光 on 15/11/17.
//  Copyright © 2015年 SHG. All rights reserved.
//

#import "MyCell.h"
@interface MyCell ()
{
    CGPoint startPoint;
    CGPoint endPoint;
}
@property(nonatomic,strong)UIView *topView;
@end
@implementation MyCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self creatdeleBtn];
        [self creatTopView];
    }
    return self;
}
- (void)creatdeleBtn{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(ScreenWidth-80, (kCellHeight - 40)/2, 60, 40);
    button.backgroundColor = [UIColor yellowColor];
    [button setTitle:@"delete" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(deleteCliek:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:button];
}
- (void)creatTopView{
    //上面的需要滑动的view
    self.topView = [[UIView alloc]initWithFrame:CGRectMake(10, 10, ScreenWidth-20, kCellHeight-10)];
    self.topView.backgroundColor = [UIColor redColor];
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
    pan.delegate = self;
    [self.topView addGestureRecognizer:pan];
    [self.contentView addSubview:self.topView];
}
-(void)setGestured:(BOOL)gestured{
    if (gestured == YES) {
        self.topView.frame = CGRectMake(-kOpenX, 10, ScreenWidth - 20, kCellHeight-10);
        /**
         这里调用block的话只有滑动打开现在的，上面打开的就会合并
         */
        if(self.sendBlock){
            self.sendBlock(self.tag);
        }else{
            NSLog(@"没有传入block");
        }
    }else{
       self.topView.frame = CGRectMake(kCardHGap, 10, ScreenWidth - 20, kCellHeight-10);
        if(self.shutBlock){
            self.shutBlock(self.tag);
        }else{
            NSLog(@"没有传入block");
        }
    }
}
- (void)deleteCliek:(UIButton *)sender{
    NSLog(@"deleted");
}
- (void)pan:(UIPanGestureRecognizer *)pan{
    /**
     这里调用block的话只要滑动，上面打开的就会合并
     */
//    if (self.sendBlock) {
//        //调用block
//        self.sendBlock(self.tag);
//    }else {
//        NSLog(@"没有传入block");
//    }
    
    if (pan.state==UIGestureRecognizerStateBegan) {
        NSLog(@"MyGestureView  gesture Began!");
        startPoint =[pan locationInView:self.superview.superview];
        
    }
    else if (pan.state==UIGestureRecognizerStateChanged){
        NSLog(@"MyGestureView  gesture Changed!");
        CGPoint point=[pan locationInView:self.superview.superview];
        
        [self  move:(point.x - startPoint.x)];
        //NSLog(@"========%ld",(point.x - startPoint.x));
    }
    else if (pan.state==UIGestureRecognizerStateEnded){
        
        NSLog(@"MyGestureView  gesture end!");
        CGPoint point=[pan locationInView:self.superview.superview];
        
        [self  endValue:(point.x - startPoint.x)];
        
    }else if (pan.state==UIGestureRecognizerStateCancelled){
        
        NSLog(@"MyGestureView  gesture Cancelled!");
    }else if (pan.state==UIGestureRecognizerStateFailed){
        
        NSLog(@"MyGestureView  gesture Failed!");
    }

}
//滑动过程中
-(void) move:(CGFloat)xValue {
    CGRect rect=self.topView.frame;
    /*
    if (startPoint.x == -kOpenX) {
        if (xValue > (-k2BtnW)+kOpenX) {
            //当打开状态下再往左移动，只能移动20
             rect.origin.x=kCardHGap+xValue;
            
        }
        if (xValue > 0) {
            //当打开状态下再往右移动，最多移动到原来的位置
            rect.origin.x=kCardHGap;
        }
    }else{
        if (xValue  <0) {//往左滑
            if (xValue > -k2BtnW) {
                //k2BtnW控制左滑的最大距离
                rect.origin.x=kCardHGap+xValue;
            }
        }else{
            rect.origin.x=kCardHGap;
        }
    }
     */
    if ((startPoint.x != -kOpenX)&&(xValue  >= 0)){
         rect.origin.x=kCardHGap;
    }else{
         rect.origin.x=kCardHGap+xValue;
    }
    self.topView.frame=rect;
}
//滑动结束
- (void) endValue:(CGFloat)xValue {
    //CGRect rect=self.topView.frame;
    if (startPoint.x == -kOpenX) {
        if (xValue <0) {
            //打开状态下当再往左移的情况下，最后回到打开状态
            
//            rect.origin.x = -80;
//            self.topView.frame = rect;
            self.gestured = YES;
        }else{
//            rect.origin.x = kCardHGap;
//            self.topView.frame = rect;
            self.gestured = NO;
        }

    }else{
        if (xValue<-kAutoGo) {
            //往左滑，最后超过－40的话就打开
//            rect.origin.x = -80;
//            self.topView.frame = rect;
            self.gestured = YES;
        }else{
//            rect.origin.x = kCardHGap;
//            self.topView.frame = rect;
            self.gestured = NO;
        }
    }
    
}
#pragma mark -  UIGestureRecognizerDelegate 代理方法

- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer{
    //
    //    NSLog(@"--------------[%@]",[gestureRecognizer class]);
    //    return NO;
    if ([gestureRecognizer.view isKindOfClass:[UITableView class]]) {
        return YES;
    }
    else {
        if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
            CGPoint translation = [gestureRecognizer translationInView:self.superview.superview];
            return fabs(translation.x)>fabs(translation.y);
        }
        return YES;
    }
}


@end
